﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPI_Text.Models;
using WebAPI_Text.Models.Database;

namespace WebAPI_Text.Controllers
{
    //特性路由 路由配置
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ArticleController : ControllerBase
    {
        private readonly mystoreContext _db;
      /// <summary>
      /// 构造方法
      /// </summary>
      /// <param name="db"></param>
        public ArticleController(mystoreContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 添加新闻文章-数据库模型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddArticle(Article article )
        {
            _db.Articles.Add(article);
            if (article == null)
                return "没有此文章";
            _db.Articles.Add(article);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增成功";
            }
            return "新增失败";
        }

        /// <summary>
        ///添加文章——自定义请求类
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddArticle1(Models.ArticleRequest article)
        {
            if (string.IsNullOrEmpty(article.ATitle))
            {
                return"文章标题不能为空";
            }
            var model = new Article();
            model.Aid = article.Aid;
            model.Atitle = article.ATitle;
            model.Acontent = article.Acontent;
            model.Abrowse = article.Abrowse;
            model.Asource = article.Asource;
            model.AcateId = article.AcateId;
            model.PubTime = DateTime.Now;
            _db.Articles.Add(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增成功";
            }
            return "新增失败";
        }
        /// <summary>
        /// 第三种添加文章的方法
        /// </summary>
        /// <param name="Aid"></param>
        /// <param name="ATitle"></param>
        /// <param name="Acontent"></param>
        /// <param name="Abrowse"></param>
        /// <param name="Asource"></param>
        /// <param name="AcateId"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddArticle2(int Aid,string ATitle,string Acontent,int Abrowse,string Asource,int AcateId)
        {
            if (string.IsNullOrEmpty(ATitle))
            {
                return "文章标题不能为空";
            }
            var model = new Article();
            model.Aid = Aid;
            model.Atitle =ATitle;
            model.Acontent =Acontent;
            model.Abrowse =Abrowse;
            model.Asource =Asource;
            model.AcateId =AcateId;
            model.PubTime = DateTime.Now;
            _db.Articles.Add(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增成功";
            }
            return "新增失败";
        }
        /// <summary>
        /// 更新文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateArticle(Article article)
        {
            var model = _db.Articles.FirstOrDefault(x => x.Aid == article.Aid);
            if (model == null)
                return "文章不存在";
            model.Aid = article.Aid;
            model.Atitle = article.Atitle;
            model.Acontent = article.Acontent;
            model.Abrowse = article.Abrowse;
            model.Asource = article.Asource;
            model.AcateId = article.AcateId;
            model.PubTime = DateTime.Now;
            _db.Articles.Update(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "更新成功";
            }
            return "更新失败";
        }
        /// <summary>
        /// 更新文章-自定义类
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateArticle1(Models.UpdateRequest article)
        {
            var model = _db.Articles.FirstOrDefault(x => x.Aid == article.Aid);
            if (model == null)
                return "文章不存在";
            model.Aid = article.Aid;
            model.Atitle = article.ATitle;
            model.Acontent = article.Acontent;
            model.Abrowse = article.Abrowse;
            model.Asource = article.Asource;
            model.AcateId = article.AcateId;
            model.PubTime = DateTime.Now;
            _db.Articles.Update(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "更新成功";
            }
            return "更新失败";
        }
        /// <summary>
        /// 删除文章-从路径背后接的参数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string DelectArticle(int Aid)
        {
            var articles = _db.Articles.FirstOrDefault(x => x.Aid == Aid);
            if (Aid == null)
            {
                return "数据不存在";
            }
            _db.Articles.Remove(articles);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "删除成功";
            }
            return "删除失败";
        }
        /// <summary>
        /// 删除产品-以json的形式进行传输参数
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteArticle1(DeleteRequest article)
        {
            var articles = _db.Articles.FirstOrDefault(x => x.Aid ==article.Aid);
            if (article == null)
            {
                return "数据不存在";
            }
            _db.Articles.Remove(articles);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "删除成功";
            }
            return "删除失败";
        }
        /// <summary>
        /// 查询文章——根据文章id查询文章
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Article GetArticleDetail(GetArticleDetailRequest request)
        {
            var article = _db.Articles.FirstOrDefault(x => x.Aid == request.Aid);

            return article;
        }
        /// <summary>
        /// 查询文章——自定义模型DTO(根据分类、文章标题和文章内容筛选查询文章数据)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public GetArticleDetailResponse GetArticleDetail1(GetArticleDetailRequest request)
        {
            var article = _db.Articles.FirstOrDefault(x => x.AcateId == request.Aid);
            if(article != null)
            {
                var category = _db.Categories.FirstOrDefault(x => x.Id == article.AcateId);
                var response = new GetArticleDetailResponse();
                response.AcateId = (int)article.AcateId;
                response.Atitle = article.Atitle;
                response.Acontent = article.Acontent;
                if (category != null)
                {
                    response.CateName = category.CateName;
                }
                return response;
            }

            return null;
        }
        /// <summary>
        /// 查询某个ID下的所有浏览量
        /// </summary>
        /// <param name="AcateId"></param>
        /// <param name="Abrowse"></param>
        /// <returns></returns>
        [HttpPost]
        public int All(int? AcateId)
        {
            var all = _db.Articles.Where(x => x.AcateId ==AcateId).Sum(x=>x.Abrowse);
            return (int)all;
        }
        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddDZ(Models.dzRequest request)
        {
            if (string.IsNullOrEmpty(request.Aid))
            {
                return "没有点赞";
            }
            var model = new Like();
            model.Aid = request.Aid;
            model.Id = request.Id;
            model.UserId = request.UserId;
            model.UpdateTime = DateTime.Now;
            model.CreateTime = DateTime.Now;
            _db.Likes.Add(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "点赞成功";
            }
            return "点赞失败";
        }
        /// <summary>
        /// 取消点赞
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public string DelectDZ(int Id)
        {
            var like = _db.Likes.FirstOrDefault(x => x.Id == Id);
            if (Id == null)
            {
                return "取消点赞失败";
            }
            _db.Likes.Remove(like);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "取消点赞成功";
            }
            return "取消点赞失败";
        }
        /// <summary>
        /// 统计文章的点赞数
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>
        [HttpPost]
        public int AllDZ(string Aid)
        {
            var all = _db.Likes.Where(x => x.Aid == Aid).Sum(x => x.Yn);
            return (int)all;
        }
        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        //[HttpPost]
        //public GetDZDetailResponse AllDZ1(GetDZRequest request)
        //{
        //    var all = _db.Likes.Where(x => x.Aid == "").Sum(x => x.Yn);
            
        //    if (all != null)
        //    {
        //        var article = _db.Articles.FirstOrDefault(x => x.Atitle == "");
        //        var response = new GetDZDetailResponse();
        //        response.Atitle = article.Atitle;
        //        if (article != null)
        //        {
        //            response.Atitle = article.Atitle;
        //        }
        //        return response;
        //    }

        //    return null;
        //}

    }
}
